package com.shengde.jinwawaforleader.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.shengde.jinwawaforleader.MyselfBroadCastReceiver;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseFragment;
import com.shengde.jinwawaforleader.model.myJob.classList.ClassListActivity;
import com.shengde.jinwawaforleader.model.myJob.classStatus.ClassStatusActivity;
import com.shengde.jinwawaforleader.model.myJob.everyRecipe.EveryRecipeActivity;
import com.shengde.jinwawaforleader.model.myJob.instance_message.Constant;
import com.shengde.jinwawaforleader.model.myJob.instance_message.InstanceMessageActivity;
import com.shengde.jinwawaforleader.model.myJob.kingLeaderEmail.KingLeaderEmailActivity;
import com.shengde.jinwawaforleader.model.myJob.monitoring.MonitoringVideoActivity;
import com.shengde.jinwawaforleader.model.myJob.onlineEducation.OnlineEducationActivity;
import com.shengde.jinwawaforleader.model.myJob.teachResource.TeachDataActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 园长工作fragment
 */
public class MyJobFragment extends BaseFragment implements AdapterView.OnItemClickListener {

	private static final String TAG = "MyJobFragment";
	private GridView gridview_myjob;
	private TextView tv_back;
	private TextView tv_title;
	private List<Map<String, Object>> data_list;
	public SimpleAdapter sim_adapter;
	public static MyJobFragment intance;
	public MyselfBroadCastReceiver myselfBroadCastReceiver;

	// 图片封装为一个数组
	private int[] icon = {R.drawable.teachdate, R.drawable.classlist,
			R.drawable.instance_messaging, R.drawable.everyrecipe, R.drawable.monitoringvideo,
			R.drawable.kingleaderemail, R.drawable.classtatus,R.drawable.online_education};
	private String[] iconName = {"教学资源", "班级列表", "家园沟通", "每周菜谱", "实时视频", "园长信箱", "班级动态","在线教育"};

	@Override
	public View initView(LayoutInflater inflater) {

		View view = inflater.inflate(R.layout.fragment_my_job, null);
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Constant.ICON);
		intentFilter.addAction(Constant.ORIGINAL_PICTURE);

		myselfBroadCastReceiver = new MyselfBroadCastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				super.onReceive(context, intent);
				Log.i(TAG, "onReceive: action = "+intent.getAction());
				if (intent.getAction().equals(Constant.ICON)) {

					icon[2] = R.drawable.home_commoncation_r;
					sim_adapter.notifyDataSetChanged();
					Log.i(TAG, "onReceive: 123465");
				}
				if (intent.getAction().equals(Constant.ORIGINAL_PICTURE)){
					icon[2] = R.drawable.instance_messaging;
					sim_adapter.notifyDataSetChanged();
					Log.i(TAG, "onReceive: 456789");

				}
			}
		};
		getActivity().registerReceiver(myselfBroadCastReceiver, intentFilter);

		initControl(view);
		return view;
	}

	private void initControl(View view) {
		intance = this;
		tv_back = (TextView) view.findViewById(R.id.tv_back);
		tv_title = (TextView) view.findViewById(R.id.tv_title);
		gridview_myjob = (GridView) view.findViewById(R.id.gridview_myjob);
	}

	@Override
	public void initData(Bundle savedInstanceState) {
		tv_back.setVisibility(View.INVISIBLE);
		tv_title.setText(getString(R.string.king_leader_job));
		//新建List
		data_list = new ArrayList<Map<String, Object>>();
		//获取数据
		getData();
		//新建适配器
		String[] from = {"image", "text"};
		int[] to = {R.id.image, R.id.text};
		sim_adapter = new SimpleAdapter(acticity, data_list, R.layout.school_yuan, from, to);
		//配置适配器
		gridview_myjob.setAdapter(sim_adapter);
	}

	public List<Map<String, Object>> getData() {
		//cion和iconName的长度是相同的，这里任选其一都可以
		for (int i = 0; i < icon.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("image", icon[i]);
			map.put("text", iconName[i]);
			data_list.add(map);
		}
		return data_list;
	}

	@Override
	public void listener() {
		gridview_myjob.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		switch (position) {
			case 0:
				Intent intent = new Intent(acticity,
						TeachDataActivity.class);
				acticity.startActivity(intent);
				break;
			case 1:
				intent = new Intent(acticity, ClassListActivity.class);
				acticity.startActivity(intent);
				break;
			case 2:
				intent = new Intent(acticity,
						InstanceMessageActivity.class);
				acticity.startActivity(intent);
				icon[2] = R.drawable.instance_messaging;
				sim_adapter.notifyDataSetChanged();
				break;
			case 3:
				intent = new Intent(acticity,
						EveryRecipeActivity.class);
				acticity.startActivity(intent);
				break;
			case 4:
				intent = new Intent(acticity,
						MonitoringVideoActivity.class);
				acticity.startActivity(intent);
				break;
			case 5:
				intent = new Intent(acticity,
						KingLeaderEmailActivity.class);
				acticity.startActivity(intent);
				break;
			case 6:
				intent = new Intent(acticity,
						ClassStatusActivity.class);
				acticity.startActivity(intent);
				break;
			case 7:
//				Toast.makeText(acticity, "暂未开通", Toast.LENGTH_SHORT).show();
				intent = new Intent(acticity,
						OnlineEducationActivity.class);
				acticity.startActivity(intent);
				break;

		}
	}

	@Override
	public void onWidgetClick(View view) {

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.i(TAG, "onDestroy: destory");
		getActivity().unregisterReceiver(myselfBroadCastReceiver);
	}
}
